package dev.ece.suit.hexmap;

import dev.ece.core.texture.Texture;
import dev.ece.core.vertex.VBO;

@Deprecated
public class HexMapChunks {

	public static final int DEFAULT_CHUNK_ROW_SIZE = 5;
	
	public static final int DEFAULT_CHUNK_COL_SIZE = 5;
	
	private HexMapChunk[][] chunks;
	
	private int rows;
	
	private int cols;
	
	private int chunk_row_size;
	
	private int chunk_col_size;
	
	private HexMapCellVAO hexMapCellVAO;
	
	private HexMapWaterCellVAO hexMapWaterCellVAO;
	
	private HexMapCreviceVAO hexMapCreviceVAO;
	
	private HexMapTriangleVAO hexMapTriangleVAO;
	
	public HexMapChunks(int cell_rows, int cell_cols) {
		this(cell_rows, cell_cols, DEFAULT_CHUNK_ROW_SIZE, DEFAULT_CHUNK_COL_SIZE);
	}
	
	public HexMapChunks(int cell_rows, int cell_cols, int chunk_row_size, int chunk_col_size) {
		this.chunk_row_size = chunk_row_size;
		this.chunk_col_size = chunk_col_size;
		rows = cell_rows / chunk_row_size;
		cols = cell_cols / chunk_col_size;
		rows = cell_rows % chunk_row_size > 0 ? rows + 1 : rows;
		cols = cell_cols % chunk_col_size > 0 ? cols + 1 : cols;
		chunks = new HexMapChunk[rows][cols];
		for(int row=0; row<rows; row++) {
			for(int col=0; col<cols; col++) {
				chunks[row][col] = new HexMapChunk();
			}
		}
	}
	
	public void setup(HexMapModel model) {
		hexMapCellVAO = HexMapCellVAO.createHexMapCellVAO(model.getCells());
		hexMapCreviceVAO = HexMapCreviceVAO.createHexMapCreviceVAO(model.getCrevices());
		hexMapTriangleVAO = HexMapTriangleVAO.createHexMapTriangleVAO(model.getTriangles());
		hexMapWaterCellVAO = HexMapWaterCellVAO.createHexMapWaterCellVAO(model.getWaters());
		VBO.unbind();
	}
	
	public void initialize() {
		hexMapCellVAO.initialize();
		hexMapCreviceVAO.initialize();
		hexMapTriangleVAO.initialize();
		hexMapWaterCellVAO.initialize();
	}
	
	public void destroy() {
		if(hexMapCellVAO != null) {
			hexMapCellVAO.destroy();
		}
		if(hexMapCreviceVAO != null) {
			hexMapCreviceVAO.destroy();
		}
		if(hexMapTriangleVAO != null) {
			hexMapTriangleVAO.destroy();
		}
		if(hexMapWaterCellVAO != null) {
			hexMapWaterCellVAO.destroy();
		}
	}
	
	/**
	 * 自动隐藏
	 * @param x
	 * @param z
	 * @param distance 小于此距离显示
	 */
	public void setAutoVisible(float x, float z, float distance) {
		for(int row = 0; row<rows; row ++) {
			for(int col = 0; col<cols; col ++) {
				chunks[row][col].setAutoVisible(x, z, distance);
			}
		}
	}
	
	public void showAllChunk() {
		for(int row = 0; row<rows; row ++) {
			for(int col = 0; col<cols; col ++) {
				chunks[row][col].setVisible(true);
			}
		}
	}
	
	protected HexMapChunk getHexMapChunk(int cell_row, int cell_col) {
		int row = cell_row / chunk_row_size;
		int col = cell_col / chunk_col_size;
		return chunks[row][col];
	}
	
	public void addHexMapCell(int cell_row, int cell_col, HexMapCell cell) {
		getHexMapChunk(cell_row, cell_col).getCells().add(cell);
	}
	
	public void addHexMapWaterCell(int cell_row, int cell_col, HexMapWaterCell water) {
		getHexMapChunk(cell_row, cell_col).getWaters().add(water);
	}
	
	public void addHexMapCrevice(int cell_row, int cell_col, HexMapCrevice crevice) {
		getHexMapChunk(cell_row, cell_col).getCrevices().add(crevice);
	}
	
	public void addHexMapTriangle(int cell_row, int cell_col, HexMapTriangle triangle) {
		getHexMapChunk(cell_row, cell_col).getTriangles().add(triangle);
	}
	
	public void render(HexMapCellShader hexMapCellShader, Texture[] textures) {
		hexMapCellVAO.bind();
		for(int row = 0; row<rows; row ++) {
			for(int col = 0; col<cols; col ++) {
				chunks[row][col].render(hexMapCellShader, textures);
			}
		}
	}
	
	public void render(HexMapCreviceShader hexMapCreviceShader, Texture[] textures) {
		hexMapCreviceVAO.bind();
		for(int row = 0; row<rows; row ++) {
			for(int col = 0; col<cols; col ++) {
				chunks[row][col].render(hexMapCreviceShader, textures);
			}
		}
	}
	
	public void render(HexMapTriangleShader hexMapTriangleShader, Texture[] textures) {
		hexMapTriangleVAO.bind();
		for(int row = 0; row<rows; row ++) {
			for(int col = 0; col<cols; col ++) {
				chunks[row][col].render(hexMapTriangleShader, textures);
			}
		}
	}
	
	public void render(HexMapWaterShader hexMapWaterShader) {
		hexMapWaterCellVAO.bind();
		for(int row = 0; row<rows; row ++) {
			for(int col = 0; col<cols; col ++) {
				chunks[row][col].render(hexMapWaterShader);
			}
		}
	}
	
	public void renderDepthMap() {
		hexMapCellVAO.bind();
		for(int row = 0; row<rows; row ++) {
			for(int col = 0; col<cols; col ++) {
				chunks[row][col].renderCellDepthMap();
			}
		}
		hexMapCreviceVAO.bind();
		for(int row = 0; row<rows; row ++) {
			for(int col = 0; col<cols; col ++) {
				chunks[row][col].renderCreviceDepthMap();
			}
		}
		hexMapTriangleVAO.bind();
		for(int row = 0; row<rows; row ++) {
			for(int col = 0; col<cols; col ++) {
				chunks[row][col].renderTriangleDepthMap();
			}
		}
	}

	public HexMapChunk[][] getChunks() {
		return chunks;
	}
	
}
